package configuration;

import com.k2data.platform.ddm.sdk.client.KMXConfig;
import com.k2data.platform.ddm.sdk.common.ParamNames;
import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Arrays;

public class KMXServerConfig {
    private String platform_type;
    private String platform_server;
    private String platform_port;
    private String tenant;
    private String user;
    private String send_timeout_millisecs;
    private String table_name;
    private String[] tag_col_names;
    private String[] field_col_names;

    public String getPlatform_type() {
        return platform_type;
    }

    public void setPlatform_type(String platform_type) {
        this.platform_type = platform_type;
    }
    public String getPlatform_server() {
        return platform_server;
    }

    public void setPlatform_server(String platform_server) {
        this.platform_server = platform_server;
    }

    public String getPlatform_port() {
        return platform_port;
    }

    public void setPlatform_port(String platform_port) {
        this.platform_port = platform_port;
    }

    public String getTenant() {
        return tenant;
    }

    public void setTenant(String tenant) {
        this.tenant = tenant;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getSend_timeout_millisecs() {
        return send_timeout_millisecs;
    }

    public void setSend_timeout_millisecs(String send_timeout_millisecs) {
        this.send_timeout_millisecs = send_timeout_millisecs;
    }

    public String getTable_name() {
        return table_name;
    }

    public void setTable_name(String table_name) {
        this.table_name = table_name;
    }

    public String[] getTag_col_names() {
        return tag_col_names;
    }

    public void setTag_col_names(String[] tag_col_names) {
        this.tag_col_names = tag_col_names;
    }

    public String[] getField_col_names() {
        return field_col_names;
    }

    public void setField_col_names(String[] field_col_names) {
        this.field_col_names = field_col_names;
    }


    public static KMXServerConfig initFromFile(String filepath) throws FileNotFoundException {
        Yaml yaml = new Yaml();
        KMXServerConfig config = yaml.loadAs(new FileInputStream(new File(filepath)),
                KMXServerConfig.class);
        return config;
    }

    public KMXConfig getKMXConfig() {
        if (platform_server == null || platform_server.isEmpty()
                || platform_port == null || platform_port.isEmpty()
                || user == null || user.isEmpty()
                || tenant == null || tenant.isEmpty()) {
            return null;
        } else {
            KMXConfig config = new KMXConfig();
            config.put(ParamNames.TENANT, tenant);
            config.put(ParamNames.USER, user);
            config.put(ParamNames.PLATFORM_SERVER, platform_server + ":" + platform_port);
            config.put(ParamNames.SEND_TIMEOUT_MILLISECS, send_timeout_millisecs);
            return config;
        }
    }

    public boolean isValidConfig() {
        if((tenant == null || tenant.isEmpty())
            || user == null || user.isEmpty()
            || platform_server == null || platform_server.isEmpty()
            || platform_port == null || platform_port.isEmpty()
            || send_timeout_millisecs == null || send_timeout_millisecs.isEmpty()
            || table_name == null || table_name.isEmpty()
            || tag_col_names == null || tag_col_names.length==0
            || field_col_names == null || field_col_names.length==0){
            return false;
        } else {
            return true;
        }
    }

    @Override
    public String toString() {
        return "KMXServerConfig {" +
                "\nplatform_type = " + platform_type +
                "\nplatform_server = " + platform_server +
                "\nplatform_port = " + platform_port +
                "\ntenant = " + tenant +
                "\nuser = " + user +
                "\nsend_timeout_millisecs = " + send_timeout_millisecs +
                "\ntable_name = " + table_name +
                "\ntag_col_names = " + Arrays.asList(tag_col_names) +
                "\nfield_col_names = " + Arrays.asList(field_col_names) +
                "\n}";
    }
}
